Include "TabModule.bmx"
Include "ToolWindow.bmx"


Const MENU_NEW_PROJECT=101
Const MENU_OPEN_PROJECT=102
Const MENU_SAVE_PROJECT=103

Const MENU_EXIT=105

Const MENU_CUT=106
Const MENU_COPY=107
Const MENU_PASTE=108

Const MENU_ABOUT=109


Type Editor_Main

	Field MainWindow : TGadget
	Field WinSize : UIRect
	Field WinFlags : Int = WINDOW_TITLEBAR|WINDOW_MENU|WINDOW_STATUS|WINDOW_CENTER

	Field MenuItems : TGadget[]
	Field Modules : Tab_Module[]
	
	Field Module_Gadget : TGadget

	
	Field Tool_Windows : TList = New TList

	


	Method New()
	
		WinSize = New UIRect
		MainWindow = CreateWindow( AppTitle, WinSize.x,WinSize.y,WinSize.w,WinSize.h, Null, WinFlags )
		
	
	End Method


	Method Init()
	
		Local mods : Tab_Module[5]
		
		SetWindow(New_Rect(0,0,(DesktopWidth()/4)*3,(DesktopHeight()/4)*3))	
		SetMenu()
		Module_Gadget = CreateTabber(0,0,ClientWidth(MainWindow),ClientHeight(MainWindow),MainWindow)
		SetGadgetLayout Module_Gadget,1,1,1,1 
		
		Local Maped : MapEditor_Module = New MapEditor_Module
		
		Maped.AddModule(Module_Gadget,True)
		Maped.Set()
		mods[0] = Maped
		AddGadgetItem Module_Gadget,"Game Editor",False,-1,"",Maped

		
		Modules = mods
	End Method
	
	
	
	Method New_Rect : UIRect(x,y,w,h)
	
		Local tmp_rect : UIRect = New UIRect
		tmp_rect.Set(x,y,w,h)
		Return tmp_rect
	
	End Method

	Method SetWindow(R : UIRect)
	
		If(R) Then
			WinSize = R
			SetGadgetShape( MainWindow,R.x,R.y,R.w,R.h )
		EndIf
	
	End Method

	Method UpdateUI()

		Local EID : Int = EventID()
		Local Obj : Object = EventSource()
		Local EDAT : Int = EventData()
		Local EEX : Object = EventExtra()
		WaitEvent()
		'DebugLog CurrentEvent.ToString()
		Select EID
			Case EVENT_APPTERMINATE 
				End
			Case EVENT_WINDOWCLOSE
				If Obj = MainWindow Then End

			Case EVENT_MENUACTION
				DebugLog "Menu "+EDAT
				Select EDAT
				
					Case MENU_EXIT
						End

					Case MENU_NEW_PROJECT
						
						Local NProjWin : NewProjectWindow = New NewProjectWindow
						NProjWin.SetAll("New Project",0,0,400,200,Tool_windows)
						NProjWin.Set()
						ListAddLast(Tool_Windows,ToolWindow(NProjWin))
				End Select
		
		End Select
		For Local i : Int = 0 To Modules.length-1
			If Modules[i] <> Null Then
				Modules[i].Update(EID,Obj,EEX)
			EndIf
		Next
		For Local TWin : ToolWindow = EachIn Tool_Windows
		
			'TWin.Update(EID,OBJ)
			Select TWin.ToolType
			
				Case 2
					Local NPTool : NewProjectWindow = NewProjectWindow(TWin)
					NPTool.Update(EID,OBJ,EEX)
								
			End Select
		Next
	End Method


	Method SetMenu()
	
		Local MMenu : TGadget[10]
		
		
		MMenu[0] = CreateMenu("File",0,WindowMenu(MainWindow))
		
		CreateMenu"&New Project",MENU_NEW_PROJECT,MMenu[0],KEY_N,MODIFIER_COMMAND
		CreateMenu"&Open Project",MENU_OPEN_PROJECT,MMenu[0],KEY_O,MODIFIER_COMMAND
		CreateMenu"",0,MMenu[0]
		CreateMenu"&Save Project",MENU_SAVE_PROJECT,MMenu[0],KEY_S,MODIFIER_COMMAND
		CreateMenu"",0,MMenu[0]
		CreateMenu"E&xit",MENU_EXIT,MMenu[0],KEY_F4,MODIFIER_COMMAND

		
		UpdateWindowMenu MainWindow
		MenuItems = MMenu
	
	End Method
	


End Type



